package cn.com.ctcloud.saas.web;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ComputeController {

	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	private DiscoveryClient client;

	@RequestMapping(value = "/sub", method = RequestMethod.GET)
	public String sub(@RequestParam final Integer a, @RequestParam final Integer b) {
		final ServiceInstance instance = client.getLocalServiceInstance();
		final Integer r = a - b;
		logger.info("/sub, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
		return "From Service-B, Result is " + r + " \nPort:" + instance.getPort();
	}

	//call service-A
	@RequestMapping(value = "testServiceA", method = RequestMethod.GET)
	public String testServiceB(@RequestParam final Integer a, @RequestParam final Integer b) {
		final RestTemplate restTemplate = new RestTemplate();
		return restTemplate.getForObject("http://localhost:2222/add?a=" + a + "&b=" + b, String.class);
	}
}